import { _decorator, Button, Label, Node, RichText, Sprite, tween, UITransform, Vec3 } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { DW_MzCfg, DW_WtCfg, DW_ZqCfg, DWTYPE, Respones } from '../../Config/GameCfg';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { ThemeMatchPPItem, ThemeMatchStarRes } from '../../../AssetPackage/Props/Hall/Game/GameZT';
import { IThemeMatchPPItem, IThemeMatchStarRes } from '../proto/gameZt';
import { GameData } from '../../Data/GameData';
import { GameZt } from './GameZt';
const { ccclass, property } = _decorator;

/**
 * 主题赛 匹配成功
 */
@ccclass('GamePPSuccess_Ctr')
export class GamePPSuccess_Ctr extends GameZt {
    nodeUp: Node;
    nodeDown: Node;

    isClick: boolean;
    btnStart: Button;
    dwType: number;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.nodeUp = this.ViewNode('bg/itemUp');
        this.nodeDown = this.ViewNode('bg/itemDown')

        this.AddButtonListener('btnBack',this,this.OnBtnCloseClick1);
        this.AddButtonListener('bg/btn',this,this.onBtnStartClick);

        this.btnStart = this.ViewComponent('bg/btn',Button);
        this.btnStart.interactable = false;

    }

    private InitData(){        
        this.isClick = true;
        this.setBgAni(0);
        this.showOpenAni();
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchStarRes,this.onThemeMatchStarRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchStarRes,this.onThemeMatchStarRes,this);
    }

    // private OnBtnCloseClick1(){
    //     UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameStart1/GameStart1',UIType.Full);
    // }

    //展示动画
    private showOpenAni(){
        let widget = screen.width;
        let time = 1;
        tween().target(this.nodeUp).hide()
        .to(0,{position: new Vec3(-widget/2 - this.nodeUp.getComponent(UITransform).width/2 ,this.nodeUp.position.y,0)}).show()
        .to(time,{position: new Vec3(0,this.nodeUp.position.y,0)},{ easing: "elasticInOut" })
        .start()

        tween().target(this.nodeDown).hide()
        .to(0,{position: new Vec3(widget/2 + this.nodeDown.getComponent(UITransform).width/2 ,this.nodeDown.position.y,0)}).show()
        .to(time,{position: new Vec3(0,this.nodeDown.position.y,0)},{ easing: "elasticInOut" })
        .call(()=>{
            this.btnStart.interactable = true;
        })
        .start()
    }

    //开始游戏
    private onBtnStartClick(){
        if(this.isClick == false){
            return;
        }
        this.isClick = false;
        globalThis.gameMgr._GameZtMsg.ThemeMatchStarReq();
    }

    private setPlayerData(node: Node,data:ThemeMatchPPItem){
        let sphead = node.getChildByPath('head/sphead').getComponent(Sprite);
        let labName = node.getChildByPath('labName').getComponent(Label);
        let labRank = node.getChildByPath('labRank').getComponent(Label);
        let txt = node.getChildByPath('spBg/RichText').getComponent(RichText);
        this.setHeadMat(sphead,1);

        if(this.hasProInObj(data,'avator')){
            this.LoadRemoteImage(data.avator, sphead);
        }
        if(this.hasProInObj(data,'name')){
            labName.string = data.name;
        }
        if(this.hasProInObj(data,'dwLv')){
            labRank.string = `全国${data.rank}名`;
        }

        if(this.hasProInObj(data,'dwType')){
            let strDw = "";
            if(data.dwType === DWTYPE.DW_WENTI){
                strDw = DW_WtCfg[data.dwLvType];
            }else if(data.dwType === DWTYPE.DW_ZUQIU){
                strDw = DW_ZqCfg[data.dwLvType];
            }else if(data.dwType === DWTYPE.DW_MINGZU){
                strDw = DW_MzCfg[data.dwLvType];
            }
    
            let des = `<color=#F2F2F2><size=28>${strDw}</size></color><color=#FFE078><size=32>LV${data.dwLv}</size></color><color=#F2F2F2><size=28>胜场：</size></color><color=#FFD251><size=32>${data.winCount}</size></color>`;
            txt.string = des;
        }
    }

    public setData(list:IThemeMatchPPItem[]){
        if(list && list.length == 2){
            for (let i = 0; i < list.length; i++) {
                let item = list[i];
                if(item.userId === GameData.inst.userInfo.userId){
                    this.setPlayerData(this.nodeUp,item);
                }else{
                    this.setPlayerData(this.nodeDown,item);
                }

            }       
        }
    }

    private onThemeMatchStarRes(data: IThemeMatchStarRes){
        console.log('========onThemeMatchStarRes=================');
        let code = data.code || 0 ;
        if(code == Respones.OK){
         
        }else{
            console.log('=====onThemeMatchStarRes err code========',code)
        }
    }
}


